﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Psm.Container;
using System.Diagnostics;
using Psm.PubSub.Messaging;
using Psm.Client;
using System.Reflection;

namespace SubscriptionSim
{
    class Program
    {   
        static void Main(string[] args)
        {
            SetConsole();

            Repository.Initialize();
            Subscription sub = new Subscription();
            Psm.PubSub.Client.PubSubClient client = new Psm.PubSub.Client.PubSubClient("DOWN_STREAM",  sub);
            QueryMessage message = client.CreateQueryMessage("TOPIC1");
            //message.Filters.Add("00001", "1");
            client.SendMessage(message);
            client.Flush();
            Console.ReadKey();
            client.Close();
        }

        private static void SetConsole()
        {
            Console.SetBufferSize(500, 2000);
            Console.SetWindowPosition(0, 0);
            Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Title = Process.GetCurrentProcess().ProcessName;
        }
    }

    public class Subscription : IMessageCallback
    {
        int TARGET_MESSAGE_COUNT = 1000000;
        Stopwatch stopwatch = Stopwatch.StartNew();
        private int count = 0;
        public void NewMessage(Psm.Messaging.IMessage message)
        {
            DataMessage dataMessage = message as DataMessage;
            if (dataMessage != null)
            {
                count++;
                if (TARGET_MESSAGE_COUNT == count)
                {
                    stopwatch.Stop();
                    Console.WriteLine("{0} messages received in {1}ms", TARGET_MESSAGE_COUNT, stopwatch.ElapsedMilliseconds);
                }
                //Console.WriteLine("Total messages {0}ms", count);
            }
        }
    }

}
